package com.iyoudoock.citymine.activity.user;

import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.activity.MainActivity;
import com.iyoudoock.citymine.activity.MapActivity;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.kvin.toolkit.utils.JumpUtils;
import com.kvin.toolkit.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.user_name_et)
    EditText userNameEt;
    @BindView(R.id.pwd_et)
    EditText pwdEt;
    @BindView(R.id.login_btn)
    Button loginBtn;
    @BindView(R.id.store_btn)
    Button registerBtn;
    @BindView(R.id.forget_pwd_tv)
    TextView forgetPwdTv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }

    @Override
    public void init() {
        this.loginBtn.setOnClickListener(mOnClickListener);
        this.registerBtn.setOnClickListener(mOnClickListener);
        this.forgetPwdTv.setOnClickListener(mOnClickListener);



    }

    @Override
    protected void onResume() {
        super.onResume();
        phoneNo = mPersistentUtils.readString(Key.User.TELEPHONE);//login name is your registered phone no
        if (!TextUtils.isEmpty(phoneNo)) {
            this.userNameEt.setText(phoneNo);
        }
    }

    @Override
    public void initNav() {
        setTitle("拾起卖");

    }


    /**
     * click event here
     */
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.login_btn:
                    phoneNo = userNameEt.getText().toString();
                    userPsw = pwdEt.getText().toString();
                    if (TextUtils.isEmpty(phoneNo) || TextUtils.isEmpty(userPsw)) {
                        ToastUtils.simpleToast(getActivity(), "用户名或密码为空");
                        return;
                    }
                    login(phoneNo, userPsw);
                    break;
                case R.id.store_btn:
                    JumpUtils.simpleJump(getActivity(), RegisterActivity.class);
                    break;
                case R.id.forget_pwd_tv:
                    JumpUtils.simpleJump(getActivity(), ForgetPswActivity.class);
                    break;
            }
        }
    };


    @Override
    public void onBackClick(View view) {
        this.finish();
    }


    @Override
    public void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        switch (msg.what) {
            case Value.Message.LOGIN_SUCCESS:
                mRoleType = mPersistentUtils.readString(Key.User.ROLE_TYPE);
                if (!mRoleType.equals(Value.Role.USER)) {
                    JumpUtils.simpleJump(getActivity(), MainActivity.class,true);
                    return;
                }
                JumpUtils.simpleJump(getActivity(), MapActivity.class,true);
                break;
            case Value.Message.LOGIN_FAILURE:
                //handle error
                break;
        }
    }
}
